# arg 1:  the new package version
# arg 2:  the old package version

RELEASE=3.17rc7-1
KERNEL_VERSION=3.17.0-rc7-MANJAROed
KERNEL_BASE=3.17
BASEVER=317
ARCH=x86_64

boobies () {
  echo "prepending CPU microcode to /boot/initramfs-${BASEVER}-${ARCH}.img"
  local destfirst="/tmp/initrd/"
  local destmc="${destfirst}/kernel/x86/microcode/"
  mkdir -p "${destmc}"
  ( cp -f "/lib/firmware/amd-ucode/microcode_amd.bin" "${destmc}/AuthenticAMD.bin" && cd "${destfirst}" && find . | cpio -o -H newc > "../ucode.cpio" 2>/dev/null && cd .. && cat "ucode.cpio" "/boot/initramfs-${BASEVER}-${ARCH}.img" > "/tmp/initramfs-${BASEVER}-${ARCH}.img" && mv -f "/tmp/initramfs-${BASEVER}-${ARCH}.img" "/boot/initramfs-${BASEVER}-${ARCH}.img")
}

rebuild_fglrx () {
  #
	#note to self, I don't need catalyst-hook.service so: sudo systemctl disable catalyst-hook
	# this exe is installed by catalyst-hook package
	cbmexe="$(which catalyst_build_module)"
	if [ -x ${cbmexe} ]; then
		echo "recompiling fglrx module for ${KERNEL_VERSION} :"
		${cbmexe} ${KERNEL_VERSION}
		local ec=$?
		if [[ $ec -eq 0 ]]; then
			echo "success."
		else
			#TODO: make errors be red so it's more obvious
			echo "failed!"
		fi
		return $ec
	fi
}

run_dkms() {
	# for the cases when dkms isn't patched to auto rebuild when kernel compile.h changes
	# and/or for the cases when systemd-modules-load.service loads existing modules before dkms gets the chance to rebuild them (quite possibly 'causing an oops, for the case when rebuilt kernel with debugging locks for example) which is still the case on my manjaro dist
	dkmsexe=$(which dkms)
	if [ ! -x "$dkmsexe" ]; then
		echo -n "failed to find dkms executable, "
		if pacman -Qq dkms; then
			#TODO: make errors be red color
			echo "aborting"
			exit 33
		else
			echo "ignoring because you don't have the package installed"
		fi		
	else
		${dkmsexe} autoinstall -k "$KERNEL_VERSION"
		local ec=$?
		#too bad it doesn't return non-zero when failed to build/install
		if [[ $ec -eq 0 ]]; then
			echo "good."
		else
			#TODO: make red
			echo "bad."
		fi
	fi
}

post_install () {
  # updating module dependencies
  echo ">>> Updating module dependencies. Please wait ..."
  depmod ${KERNEL_VERSION}
  echo ">>> Generating initial ramdisk, using mkinitcpio.  Please wait..."
  mkinitcpio -p linux${BASEVER}

  echo ">>> WARNING: AT keyboard support is no longer built into the kernel."
  echo ">>>          In order to use your keyboard during early init, you MUST"
  echo ">>>          include the 'keyboard' hook in your mkinitcpio.conf."

	boobies
  
  # update grub.cfg
  if [ -f /usr/sbin/update-grub ]; then
      update-grub
  else
      echo "WARNING: It seems that grub is not installed - Your system might not boot."
      echo "Add followed initramfs files to your bootloader config:"
      echo ">> linux	/boot/vmlinuz-${BASEVER}-${ARCH}"
      echo ">> initrd	/boot/initramfs-${BASEVER}-${ARCH}.img"
  fi

	rebuild_fglrx
	run_dkms
}

post_upgrade() {
  # reminder notice
  if findmnt --fstab -uno SOURCE /boot &>/dev/null && ! mountpoint -q /boot; then
      echo "WARNING: /boot appears to be a separate partition but is not mounted."
  fi

  if [ $(vercmp $2 3.13) -lt 0 ]; then
    echo ">>> WARNING: AT keyboard support is no longer built into the kernel."
    echo ">>>          In order to use your keyboard during early init, you MUST"
    echo ">>>          include the 'keyboard' hook in your mkinitcpio.conf."
  fi
  
  # remove old initcpio
  rm -f /boot/initramfs-${KERNEL_BASE}*-${ARCH}*.img
  rm -f /boot/initramfs-${BASEVER}-${ARCH}*.img
  
  # updating module dependencies
  echo ">>> Updating module dependencies. Please wait ..."
  depmod ${KERNEL_VERSION}
  echo ">>> Generating initial ramdisk, using mkinitcpio.  Please wait..."
  mkinitcpio -p linux${BASEVER}

	boobies
  
  # update grub.cfg
  if [ -f /usr/sbin/update-grub ]; then
      update-grub
  else
      echo "WARNING: It seems that grub is not installed - Your system might not boot."
      echo "Add followed initramfs files to your bootloader config:"
      echo ">> linux	/boot/vmlinuz-${BASEVER}-${ARCH}"
      echo ">> initrd	/boot/initramfs-${BASEVER}-${ARCH}.img"
  fi

	rebuild_fglrx
	run_dkms

}

post_remove() {
  # remove initcpio
  rm -f /boot/initramfs-${BASEVER}-${ARCH}*.img
  
  # update grub.cfg
  if [ -f /usr/sbin/update-grub ]; then
      update-grub
  fi
}
